<template>
  <div class="fee-summary">
    <div class="fee-summary-item">
      <div class="fee-summary-item-amount">
        {{ feeSummaryTitle }}：<text class="amount">{{ sumFee }}</text
        ><text class="unit">元</text>
      </div>
      <div class="fee-summary-item-toggle" @tap="viewDetailFn">
        查看明细
        <ve-icon v-if="!toggleDetailStatus" class="icon" name="toggle-up" width="32" height="32" />
        <ve-icon v-else class="icon" name="toggle-down" width="32" height="32" />
      </div>
    </div>
    <!-- 续租原费用信息 开始 -->
    <div class="fee-summary-item" v-if="showOriginFeeStatics">
      <div class="fee-summary-item-amount">
        原费用信息：<text class="amount">{{ originSumInfo?.sum }}</text
        ><text class="unit">元</text>
      </div>
      <div class="fee-summary-item-toggle" @click="viewOriginFeeInfo">
        查看详情<ve-icon class="icon" name="arrow-right" width="32" height="32" />
      </div>
    </div>
    <!-- 续租原费用信息 结束 -->
    <!-- 查看明细，只展示一级 -->
    <ve-popup
      v-model="toggleDetailStatus"
      @close="toggleDetailStatus = false"
      mode="bottom"
      :customStyle="{ marginBottom: offsetBottom + 'px' }"
    >
      <div class="fee-static-wrap">
        <div class="fee-static-header">费用明细</div>
        <div class="fee-static-content">
          <div class="fee-static-item" v-for="(item, index) in feeTreeStData" :key="index">
            <div class="fee-static-item-field">
              <div class="fee-name">{{ item.name }}</div>
              <div class="fee-value">{{ unitVal(item.value) }}</div>
            </div>
          </div>
        </div>
      </div>
    </ve-popup>
  </div>
</template>
<script setup>
  import { ref, inject, computed, watch, onMounted } from 'vue'
  import { buildTreeData, processTreeData } from './fee-statistic.js'
  import { decimalAddition, unitVal } from '@/utils'
  const props = defineProps({
    feeSummaryTitle: {
      type: String,
      default: '费用合计',
    },
    // 是否显示原费用信息统计（续租用）
    showOriginFeeStatics: {
      type: Boolean,
      default: false,
    },
    // 原费用合计（续租用）
    originSumInfo: Object,
  })

  const toggleDetailStatus = ref(false)
  const feeForm = inject('feeForm')
  // 查看明细
  const viewDetailFn = () => {
    toggleDetailStatus.value = !toggleDetailStatus.value
  }

  // 实时费用信息开始
  const feeRealtimeTreeData = computed(() => {
    let orderFee = feeForm.value.orderFee
    if (orderFee) {
      return buildTreeData(orderFee)
    }
    return []
  })
  // 此处 feeTreeStData 为一个 “ 常量 ”，并未直接将 feeTreeStData 逻辑放到 feeRealtimeTreeData computed 逻辑中
  // 原因是由于费用名字点击展开收起无法操纵 computed 出来的数据，不生效？（存疑）
  const feeTreeStData = ref([])
  watch(
    () => feeRealtimeTreeData.value,
    (newVal) => {
      feeTreeStData.value = processTreeData(newVal, feeTreeStData)
    },
    { immediate: true, deep: true }
  )
  // 实时费用信息结束

  // 费用合计
  const sumFee = computed(() => {
    let sum = 0
    feeTreeStData.value.forEach((item) => {
      sum = decimalAddition(sum, item.value)
    })
    return sum
  })

  const offsetBottom = ref(260)
  onMounted(() => {
    const query = uni.createSelectorQuery().in(this)
    query
      .select('#footer-box')
      .boundingClientRect((rect) => {
        let { height } = rect
        offsetBottom.value = height
      })
      .exec()
  })

  // 查看原费用信息
  const viewOriginFeeInfo = () => {
    const originSumInfo = props.originSumInfo
    // 存储到 storage 中，原费用信息页面不需要处理逻辑
    uni.setStorageSync('sOrderOriginFeeSumInfo', originSumInfo)
    uni.navigateTo({ url: '/pages/order/short-order/relet-order-origin-fee' })
  }
</script>
<style lang="less" scoped>
  .fee-summary {
    .fee-summary-item {
      display: flex;
      align-items: center;
      height: 112rpx;
      line-height: 110rpx;
      border-bottom: 2rpx solid rgba(216, 216, 216, 0.3);
      .fee-summary-item-amount {
        font-size: 32rpx;
        color: #1e1e1e;
        flex: 1;
        .amount {
          font-size: 48rpx;
          font-weight: 600;
          line-height: 1;
          color: #fe615a;
        }
        .unit {
          color: #fe615aff;
          font-size: 24rpx;
          vertical-align: bottom;
          margin-left: 8rpx;
        }
      }
      .fee-summary-item-toggle {
        height: 44rpx;
        display: flex;
        align-items: center;
        font-size: 28rpx;
        color: #3072f6;
        .icon {
          margin-left: 16rpx;
        }
      }
    }
  }

  .fee-static-wrap {
    padding: 0 32rpx;
    .fee-static-header {
      height: 76rpx;
      line-height: 100rpx;
      font-size: 30rpx;
      font-weight: 600;
      color: #191c27;
    }
    .fee-static-content {
      .fee-static-item {
        .fee-static-item-field {
          display: flex;
          justify-content: space-between;
          border-bottom: 2rpx solid rgba(216, 216, 216, 0.3);
          height: 108rpx;
          line-height: 106rpx;
        }
        .fee-name {
          color: #8491a8ff;
        }
        .fee-value {
          color: #191c27ff;
        }
      }
    }
  }
</style>
